package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Database;
import com.ruoyi.system.service.IDatabaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据库管理控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dbmanagement")
public class DbManagementController extends BaseController
{
    @Autowired
    private IDatabaseService databaseService;

    /**
     * 查询数据库列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(Database database)
    {
        startPage();
        List<Database> list = databaseService.selectDatabaseList(database);
        return getDataTable(list);
    }

    /**
     * 获取数据库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:query')")
    @GetMapping(value = "/{dbId}")
    public AjaxResult getInfo(@PathVariable("dbId") Long dbId)
    {
        return AjaxResult.success(databaseService.selectDatabaseById(dbId));
    }

    /**
     * 新增数据库
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:add')")
    @Log(title = "数据库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Database database)
    {
        return toAjax(databaseService.insertDatabase(database));
    }

    /**
     * 修改数据库
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:edit')")
    @Log(title = "数据库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Database database)
    {
        return toAjax(databaseService.updateDatabase(database));
    }

    /**
     * 删除数据库
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:remove')")
    @Log(title = "数据库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dbIds}")
    public AjaxResult remove(@PathVariable Long[] dbIds)
    {
        return toAjax(databaseService.deleteDatabaseByIds(dbIds));
    }
    
    /**
     * 测试数据库连接
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:test')")
    @Log(title = "数据库管理", businessType = BusinessType.OTHER)
    @GetMapping("/test/{dbId}")
    public AjaxResult testConnection(@PathVariable("dbId") Long dbId)
    {
        return databaseService.testDatabaseConnection(dbId);
    }
    
    /**
     * 获取数据库表列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:query')")
    @GetMapping("/tables/{dbId}")
    public AjaxResult listTables(@PathVariable("dbId") Long dbId)
    {
        return AjaxResult.success(databaseService.listDatabaseTables(dbId));
    }
    
    /**
     * 备份数据库
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:backup')")
    @Log(title = "数据库管理", businessType = BusinessType.OTHER)
    @PostMapping("/backup")
    public AjaxResult backupDatabase(@RequestBody Database database)
    {
        return databaseService.backupDatabase(database);
    }
    
    /**
     * 还原数据库
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:restore')")
    @Log(title = "数据库管理", businessType = BusinessType.OTHER)
    @PostMapping("/restore")
    public AjaxResult restoreDatabase(@RequestBody Database database)
    {
        return databaseService.restoreDatabase(database);
    }
    
    /**
     * 获取备份历史
     */
    @PreAuthorize("@ss.hasPermi('system:dbmanagement:query')")
    @GetMapping("/backupHistory/{dbId}")
    public AjaxResult getBackupHistory(@PathVariable("dbId") Long dbId)
    {
        return AjaxResult.success(databaseService.getBackupHistory(dbId));
    }
} 